package cheerly.centershow;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    private CenterShowHorizontalScrollView mScrollView;
    private final int middlePadding = 20;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mScrollView = (CenterShowHorizontalScrollView) findViewById(R.id.scrollView);

        findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mScrollView.getLinear().removeAllViews();

                for (int i = 0; i < 15; i++) {
                    final View titleItem = View.inflate(MainActivity.this, R.layout.work_time_title_item, null);
                    mScrollView.addItemView(titleItem, i);

                    LinearLayout.LayoutParams titleParams = (LinearLayout.LayoutParams) titleItem.getLayoutParams();
                    titleParams.leftMargin = middlePadding / 2;
                    titleParams.rightMargin = middlePadding / 2;

                    TextView textView = (TextView) titleItem.findViewById(R.id.text);
                    textView.setText("我是第" + i + "个");

                    titleItem.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            mScrollView.onClicked(v);
                        }
                    });
                }
            }
        });
    }
}
